/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.DCclient.servlet;

import fr.DCclient.utils.AuthManager;
import fr.annuaireComp.entities.Personnel;
import java.io.IOException;
import java.io.PrintWriter;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author MiniK
 */
@WebServlet(name = "login", urlPatterns = {"/login"})
public class login extends HttpServlet {

    @EJB
    private fr.annuaireComp.sessions.PersonnelManager persMan;

 
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //  processRequest(request, response);
        String pageRequested = request.getParameter(AuthManager.LOGIN_REFERER_URI_PARAM);
        //   String errorType = request.getParameter(AuthManager.LOGIN_ERROR_TYPE_URI_PARAM);

        if (pageRequested != null) {
            request.setAttribute(AuthManager.ERROR_MESSAGE_LOGIN_PAGE_PARAM_NAME, "Pour acceder à cette page, vous devez d'abord vous connecter avec un compte autorisé.");
            request.setAttribute("targeturl", pageRequested);
        }
        request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String username = request.getParameter("email");
        String password = request.getParameter("password");
        String referer = request.getParameter("targeturl");
        // System.out.println("referer : "+request.getHeader("referer"));
        if (username != null && password != null) {
            Personnel p = persMan.authenticatePersonnel(username.trim(), password.trim());
            if (p != null) {
                AuthManager.setCurrentUser(request, p);
                response.sendRedirect(referer);
            } else {
                request.setAttribute("errormsg", "Mot de passe ou login incorrect");
                request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
            }
        } else {
            request.setAttribute("errormsg", "Mot de passe ou login incorrect");
            request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
    
}
